package com.ruyuan.eshop.common.service.trade.order.impl;

import com.ruyuan.eshop.common.framework.exception.ServiceException;
import com.ruyuan.eshop.common.framework.util.CurrencyUtil;
import com.ruyuan.eshop.common.model.errorcode.PaymentErrorCode;
import com.ruyuan.eshop.common.model.member.dos.MemberWalletDO;
import com.ruyuan.eshop.common.model.trade.order.vo.BalancePayVO;
import com.ruyuan.eshop.common.service.trade.order.BalanceManager;
import com.ruyuan.eshop.common.service.trade.order.TradeQueryManager;
import com.ruyuan.eshop.common.service.trade.order.plugin.PaymentServicePlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预存款抵扣业务类
 **/
@Service
public class BalanceManagerImpl implements BalanceManager {


    @Autowired
    private TradeQueryManager tradeQueryManager;

    @Autowired
    private List<PaymentServicePlugin> paymentServicePlugin;


    /**
     * 预存款抵扣方法
     * @param sn   交易编号/订单编号
     * @param memberId 会员id
     * @param tradeType 交易类型
     * @param password  交易密码
     * @return 预存款支付参数
     */
    @Override
    public BalancePayVO balancePay(String sn, Long memberId, String tradeType, String password) {

        //在支付子业务插件中，找到对应业务插件
        PaymentServicePlugin plugin = this.findServicePlugin(tradeType);
        //判断是否可支付状态
        boolean isLegitimate = plugin.checkStatus(sn, 0);

        //判断订单状态是否已确认可以订单支付，否抛出异常
        if (!isLegitimate) {
            throw new ServiceException(PaymentErrorCode.E506.code(), "该交易状态不正确，无法支付");
        }

        //获取订单待支付金额
        Double needPay = plugin.getPrice(sn);

        //获取会员预存款信息
        MemberWalletDO walletDO = createMemberWallet();

        //判断预存款余额与订单待支付金额
        Double diffPrice = CurrencyUtil.sub(needPay,walletDO.getPreDeposite());
        Double balance = 0D;
        if(diffPrice >= 0 ){
            //此时预存款不足，无法完全抵扣所有订单支付基恩
            balance = walletDO.getPreDeposite();
            needPay = diffPrice;

        }else{
            //此时订单支付金额为0
            balance = needPay;
            needPay = 0D;

        }

        BalancePayVO payVO = new BalancePayVO();
        payVO.setSn(sn);
        payVO.setBalance(balance);
        payVO.setNeedPay(needPay);

        //预存款支付，修改订单待支付金额
        plugin.balancePay(payVO,memberId);

        return payVO;

    }

    private MemberWalletDO createMemberWallet() {
        // TODO

        // 模拟一份会员电子钱包数据
        MemberWalletDO memberWalletDO = new MemberWalletDO();
        memberWalletDO.setId(100L);
        memberWalletDO.setMemberId(100L);
        memberWalletDO.setMemberName("buy001");
        memberWalletDO.setPreDeposite(10000000.00);
        memberWalletDO.setDepositePassword("415285");

        return memberWalletDO;
    }

    /**
     * 在支付子业务插件中  找到对应业务插件
     * @return
     */
    private PaymentServicePlugin findServicePlugin(String tradeType) {
        for (PaymentServicePlugin plugin:paymentServicePlugin){
            if (tradeType.equals(plugin.getServiceType())) {
                return plugin;
            }
        }
        return null;
    }
}
